
#include "pktinfo.h"
#include "hashmap.h"

#ifndef SESSION_MNG_H
#define SESSION_MNG_H

#define MAX_STREAM_NUM   4
#define MAX_SESSION_NUM   4
struct session_entry;

typedef int (*F_RECEIVE_FROM_STREAM)(struct session_entry *sentry,int portidx,uint8_t *buff,int size);
typedef int (*F_RECEIVE_FROM_CTL)(struct session_entry *sentry,int portidx,uint8_t *buff,int size);

typedef struct client_info
{
	UDP_CLIENT_ENTRY *udpentry;
	UDP_CLIENT_ENTRY *session_ctl;
	uint8_t *txbuff; /*send buff*/
	int size; /*send size*/
	int avalid;
	int timeout;
	CTL_PTK heartbeat;
	void *priv;
}CLIENT_INFO;

typedef struct session_info
{
	//UDP_CLIENT_ENTRY *udpentry[MAX_STREAM_NUM];
	//UDP_CLIENT_ENTRY *session_ctl[MAX_STREAM_NUM];
	CLIENT_INFO streams[MAX_STREAM_NUM];
	int portnum;
	uint8_t *totalbuff;
}SESSION_INFO;
struct session_entry;
struct session_tx_schdule;

typedef int (*F_SESSION_SCHDULE_TX)(struct session_tx_schdule *tx_schdule,uint8_t *buff,int size);
typedef int (*F_SESSION_SCHDULE_INIT)(struct session_tx_schdule *tx_schdule);
typedef int (*F_SESSION_SCHDULE)(struct session_tx_schdule *tx_schdule);


typedef struct session_tx_schdule
{
	F_SESSION_SCHDULE_TX fn_tx_schdule;
	F_SESSION_SCHDULE fn_schdule;
	F_SESSION_SCHDULE_INIT fn_schdule_init;
	struct session_entry *sentry;
	void *priv;
}SESSION_TX_SCHDULE;


typedef struct session_entry
{
	SESSION_INFO sinfo;
	int s_thread_fd;
	HASHMAP_MNG *streammng;
	F_RECEIVE_FROM_STREAM fn_receive_cb;
	F_RECEIVE_FROM_CTL fn_ctl_cb;  /* used by shcdule or user*/
	SESSION_TX_SCHDULE *tx_schdule;
	int used;
}SESSION_ENTRY;


typedef struct session_mng
{
	SESSION_ENTRY entry[MAX_SESSION_NUM];
	int usednum;
}SESSION_MNG;





void register_session_ctlcb(SESSION_ENTRY *sentry,F_RECEIVE_FROM_CTL ctl_receiver);
void register_session_datacb(SESSION_ENTRY *sentry,F_RECEIVE_FROM_STREAM data_receiver);
int read_session_by_idx(SESSION_ENTRY *sentry,int idx,uint8_t *buff,int size);
SESSION_ENTRY *create_session(int streamnum,char *destip,int destport,int ctlport);
int start_session(SESSION_ENTRY *sentry);

#endif

